<?php

require_once '../bootstrap.php';
require_once 'entities/User.php';
require_once 'entities/Visit.php';
require_once 'entities/Media.php';
require_once 'utils/util.php';
require_once 'entities/Strategy.php';
require_once 'entities/Domain.php';
require_once 'entities/ItemDomain.php';
require_once 'entities/Fare.php';
require_once 'entities/Operation.php';

function getTotalVisits($entityManager, $user) {

  $query = $entityManager->createQuery('SELECT COUNT(v.visId) FROM Visit v WHERE v.visUsuId = :vis_usu_id');
  $query->setParameter('vis_usu_id', $user);

  $result = $query->getSingleScalarResult();

  return $result;
}

function totalBTCWon($entityManager, $user) {

  $query = $entityManager->createQuery('SELECT SUM(o.opeReferalAmount) FROM Operation o WHERE o.opeUsu = :ope_usu_id');
  $query->setParameter('ope_usu_id', $user);

  $result = $query->getSingleScalarResult();

  return $result;
}

function totalBTCWonLast30Days($entityManager, $user) {

  $query = $entityManager->createQuery('SELECT SUM(o.opeReferalAmount) FROM Operation o 
                                        WHERE o.opeUsu = :ope_usu_id AND o.opeAuditCreateDate > :create_time');
  $query->setParameters(array('ope_usu_id' => $user,
                              'create_time' => time() - ( 60 * 60 * 24 * 30 ),));

  $result = $query->getSingleScalarResult();

  return $result;
}

function getStrategy($entityManager, $media) {

  $query = $entityManager->createQuery('SELECT MAX(s.strId) FROM Strategy s JOIN Media m
                                        WHERE s.strMedId=m.medId AND m.medName=:med_name
                                        AND :time BETWEEN s.strValidityStart AND s.strValidityEnd');
  $query->setParameters(array('med_name' => $media,
                              'time' => time(),
  ));

  $result = $query->getSingleScalarResult();

  return $result;
}

function getTotalReferal($entityManager, $user) {
  
  $query = $entityManager->createQuery('SELECT COUNT(u.usuReferalId) FROM User u WHERE u.usuReferalId = :user_id');
  $query->setParameter('user_id', $user);
  
  $result = $query->getSingleScalarResult();
  
  return $result;
}
